import { Node } from "../Node";
import { NodeStatus } from "../NodeStatus";
import { INode } from "../interfaces/INode";



/**
 * 装饰节点
 */
export abstract class DecoratorNode<T> extends Node<T> {

    protected $child: INode<T> | null = null;

    constructor(name: string,blackboard: T) {
        super(name,blackboard);
    }

    /**
     * 设置子节点
     * @param child 
     */
    SetChild(child: INode<T>) {
        if (this.$child != null) {
            throw new Error("Child already set");
        }
        this.$child = child;
    }

    GetChild(): INode<T> | null {
        return this.$child;
    }

    Halt(): void {
        this.HaltChild();
        this.status = NodeStatus.IDLE;
    }

    HaltChild(): void {
        if (!this.$child) {
            return;
        }
        if (this.$child.status == NodeStatus.RUNNING) {
            this.$child.Halt();
        }
        this.$child.status = NodeStatus.IDLE;
    }

    Destroy(): void {
        super.Destroy();
        if (this.$child) {
            if (this.$child.status == NodeStatus.RUNNING) {
                this.$child.Halt();
            }
            this.$child.Destroy();
            this.$child = null;
        }
    }
}